package RTL.Task4;

//集合所有接口并输出相应的回答

import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;

public class StudentInfo implements  Add, Remove, Update, Find {
    private static List<Student> students  = new ArrayList<>();
    private static StudentInfo studentInfo= new StudentInfo();

    private StudentInfo() {
    }
    public static StudentInfo getInstance() {
        return studentInfo;
    }
    public static List<Student> getStudents() {
        return students;
    }

    private Student getStudentFromAdmin() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生信息");
        System.out.print("学号");
        int id = Integer.parseInt(sc.next().strip());
        System.out.print("姓名");
        String name = sc.next().strip();
        System.out.print("年龄");
        int age = Integer.parseInt(sc.next().strip());
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        return student;
    }

    private Student getIdFromAdmin(){
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入学生学号");
        System.out.print("学号：");
        int id = Integer.parseInt(sc.next().strip());

        Student student = new Student();
        student.setId(id);
        return student;
    }

    public void list() {
        if (students.isEmpty()) {
            System.out.println("暂无学生信息");
        } else {
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }

    @Override
    public void add() {
        if (Add.add(getStudents(), getStudentFromAdmin())) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败，学号已存在");
        }
    }

    @Override
    public void remove() {
        if (Remove.remove(getStudents(), getStudentFromAdmin())) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败，学号不存在");
        }
    }

    @Override
    public void update() {
        if (Update.update(getStudents(), getStudentFromAdmin())) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败，学号不存在");
        }
    }

    @Override
    public void find() {
        Student student = Find.find(getStudents(), getIdFromAdmin());
        if (student == null) {
            System.out.println("查找失败，学号不存在");
        } else {
            System.out.println(student);
        }
    }



}
